/*
 * 
 * Version 1.00
 * 
 * Created on 2007-6-4
 * Encoding GB2312
 */
package net.comm.shequ.service.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;
import java.util.Vector;

import net.comm.shequ.bean.BaseJgShenqing;
import net.comm.shequ.db.JgShenqingServiceSqlMysql;
import net.comm.shequ.db.DbJgShenqingService;
import net.comm.shequ.web.form.JgShenqingForm;
import net.comm.shequ.web.form.WuyeForm;
import net.comm.shequ.web.form.YeweiForm;
import net.comm.common.GlbFinal;
import net.comm.common.ReturnMessage;
import net.comm.common.common;
import net.comm.file.service.impl.FilesServiceImpl;
import net.comm.file.web.form.FilesForm;

import net.comm.page.PageCtr;
import net.comm.param.service.impl.SequenceMgr;
import net.comm.user.service.impl.UsersServiceImpl;
import net.comm.user.web.form.UsersForm;
import net.comm.util.DatetimeUtil;
import net.comm.util.DbcpPool;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.sakaiproject.db.impl.BasicSqlService;


/**
 *
 * @author hcgong(hcgong@sina.com)
 * @version 1.0
 * @date 2007-6-4
 */
public class JgShenqingServiceImpl {

	static Logger logger = Logger.getLogger(JgShenqingServiceImpl.class.getName());

	/**
	 * 
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#list(net.swift.module.other.ExaQueryBean, net.swift.common.ReturnMessage)
	 */
	public List list(JgShenqingForm theForm,  ReturnMessage msg) 
	{
		List list = null;

		Connection cn = null;

		try
		{
			cn = DbcpPool.getConnection();
			
			JgShenqingServiceSqlMysql servSql = new JgShenqingServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();

			/*
			 * 获得查询SQL语句
			 */
			String sql = servSql.getCountSql2();

			/**
			 * 根据SQL执行结果计算分页相关信息
			 */
			PageCtr page = theForm.getPage();
			page.setTotalNum(bSqlServ.dbCount(cn, sql, null));
			page.caculate();

			//取得FORM中传入的查询参数
			Object[] fields2 = new Object[2];
			
			fields2[0] = page.getPos()-1;
			fields2[1] = page.getPageSize();
			
			sql = servSql.getRecbyPageSql();

			list = bSqlServ.dbRead(cn, sql, fields2, new DbJgShenqingService());
			
		    DbcpPool.returnConnection(cn);
		    
		} catch(Exception ex) 
		{
			/**
			 * 记录相关日志
			 */
			logger.error(ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
			
		} finally {
			DbcpPool.returnConnection(cn);
		}		
		
		return list;
	}

	/**
	 * 
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#list(net.swift.module.other.ExaQueryBean, net.swift.common.ReturnMessage)
	 */
	public List adlist(JgShenqingForm theForm,  ReturnMessage msg) 
	{
		List list = new Vector();

		Connection cn = null;
		Statement stmt=null;
		ResultSet rs=null;
		try
		{
			cn = DbcpPool.getConnection();
			
			JgShenqingServiceSqlMysql servSql = new JgShenqingServiceSqlMysql();
			
			StringBuffer sb = new StringBuffer(" where 1=1");

			String qryJgType = theForm.getQryJgType().replaceAll(GlbFinal.SQLINjection, " ");
			if(!qryJgType.equals("")){
				
				sb.append(" and JgType = '");
				sb.append(qryJgType);
				sb.append("' ");
			}

			String sryStatus = theForm.getQryStatus().replaceAll(GlbFinal.SQLINjection, " ");
			if(!sryStatus.equals("")){
				
				sb.append(" and Status = '");
				sb.append(sryStatus);
				sb.append("' ");
			}
			
			//查询记录数
			stmt = cn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
			String  sql = servSql.getCountSql1();

			logger.debug(sql+sb.toString());
			rs=stmt.executeQuery(sql+sb.toString());
			int nRows = 0;
			if (rs.next())
	           nRows = rs.getInt("COUNT");
			else
			{
	            rs.close();
	            stmt.close();
	            DbcpPool.returnConnection(cn);
			    return list;
			}
			/**
			 * 根据SQL执行结果计算分页相关信息
			 */
			PageCtr page = theForm.getPage();
			page.setTotalNum(nRows);
			page.caculate();

			sb.append(" limit ");
			sb.append(page.getPos()-1);
			sb.append(", ");
			sb.append(page.getPageSize());
			
			 //取记录
			 sql = servSql.getRecSql();
			 logger.debug(sql+sb.toString());
			 DbJgShenqingService dbblog = new DbJgShenqingService();
			 
			 rs=stmt.executeQuery(sql+sb.toString());
			 while (rs.next())
			 {
				try
				{
					Object obj = dbblog.readSqlResultRecord(rs);
					if (obj != null) list.add(obj);
				}
				catch (Throwable t)
				{
					logger.error(t.getMessage());
				}		
			 }
			 rs.close();
			 stmt.close();
		     DbcpPool.returnConnection(cn);

		} catch(Exception ex) 
		{
			/**
			 * 记录相关日志
			 */
			logger.error(ex.getMessage());
			try {
				if (rs != null)
					rs.close();
				if (stmt != null)
					stmt.close();
				DbcpPool.returnConnection(cn);
			} catch (Exception ex1) {
				ex1.printStackTrace();
			}			
	    }finally {
			try {
				if (rs != null)
					rs.close();
				if (stmt != null)
					stmt.close();
			} catch (Exception ex) {
				ex.printStackTrace();
			}			
			DbcpPool.returnConnection(cn);
		}		
		return list;
	}		

	/**
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#get(net.swift.db.tab_paramnostro, net.swift.common.ReturnMessage)
	 */
	public void get(JgShenqingForm theForm, ReturnMessage msg) 
	{
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			DbcpPool.beginTran(cn);
			
			List list = null;

			//取得FORM中传入的查询参数
			Object[] fields2 = new Object[1];
			
			fields2[0] = theForm.getSeqID();
			
			JgShenqingServiceSqlMysql servSql = new JgShenqingServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			String sql = servSql.getbykeySql();			
					
			list = bSqlServ.dbRead(cn, sql, fields2, new DbJgShenqingService());
			
			if(list.size() ==0){
				common.setAppFailMsg(msg , " 取记录失败，请检查！");
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;				
			}
			BaseJgShenqing base = (BaseJgShenqing)list.get(0);
			
			BeanUtils.copyProperties(theForm, base); 
			
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);
			return;
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about info" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}
	
	/**
	 * 添加到表中
	 * @see net.IUserbasicService.module.other.IUserService#add(net.swift.common.ReturnMessage)
	 */
	public void add(JgShenqingForm theForm, ReturnMessage msg) 
	{
		
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			
			DbcpPool.beginTran(cn);
			
			add(cn, theForm, msg);
			
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);

		} catch(Exception ex) 
		{
			common.setAppFailMsg(msg , ex.getMessage());
			logger.error("System error , param get about info" + ex);
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}
	/**
	 * 添加到表中
	 * @see net.IUserbasicService.module.other.IUserService#add(net.swift.common.ReturnMessage)
	 */
	public void add(Connection cn, JgShenqingForm theForm, ReturnMessage msg) 
	{
		
		try
		{
			//判断负责人ID是否存在
			UsersServiceImpl userservice = new UsersServiceImpl();
			UsersForm userForm = new UsersForm();
			
			userForm.setUserID(theForm.getUserID());
			
			userservice.get(userForm, msg);
			if(msg.getErrCode()!= 0){
				return;
			}
			
			JgShenqingServiceSqlMysql servSql = new JgShenqingServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			//保存附件一
			if(theForm.getFilepath1() != null&&!theForm.getFilepath1().getFileName().equals("")){
				
				FilesForm fileForm = new FilesForm();
				fileForm.setFormFile(theForm.getFilepath1());
				fileForm.setF_info(theForm.getJgName());
				fileForm.setF_Zip(GlbFinal.ZIP_600);
				
				FilesServiceImpl fileservice = new FilesServiceImpl();
				
				fileservice.add(cn, fileForm, msg);
				if(msg.getErrCode() != 0){
					return;
				}
				theForm.setF_ID1(fileForm.getF_ID());
			}	
			//保存附件一
			if(theForm.getFilepath2() != null&&!theForm.getFilepath2().getFileName().equals("")){
				
				FilesForm fileForm = new FilesForm();
				fileForm.setFormFile(theForm.getFilepath2());
				fileForm.setF_info(theForm.getJgName());
				fileForm.setF_Zip(GlbFinal.ZIP_600);
				
				FilesServiceImpl fileservice = new FilesServiceImpl();
				
				fileservice.add(cn, fileForm, msg);
				if(msg.getErrCode() != 0){
					return;
				}
				theForm.setF_ID2(fileForm.getF_ID());
			}				
			
			theForm.setSeqID(SequenceMgr.generateSeq( GlbFinal.SEQ_INITDATE,GlbFinal.SEQ_JGSHENQINGID));
			
			//取得FORM中传入的查询参数
			Object[] fields = new Object[11];
			
			fields[0] = theForm.getSeqID();
			fields[1] = theForm.getJgType();
			fields[2] = theForm.getJgName();
			fields[3] = theForm.getVillageID();
			fields[4] = theForm.getContent();
			fields[5] = theForm.getUserID();
			fields[6] = theForm.getTel();
			fields[7] = theForm.getF_ID1();
			fields[8] = theForm.getF_ID2();
			fields[9] = GlbFinal.STS_CHECKING;
			fields[10] = DatetimeUtil.getDatetime();
			
			String sql = servSql.getInsertSql();
						
			boolean  res = bSqlServ.dbWrite(cn, sql, fields);
			if(!res){
				common.setAppFailMsg(msg , " 添加机构申请失败！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return ;
			}
		
		} catch(Exception ex) 
		{
			common.setAppFailMsg(msg , ex.getMessage());
			logger.error("System error , param get about info" + ex);
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		}
		
		return ;
	}	

	/**
	 * 删除一条指定博客
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void delete(JgShenqingForm theForm,String path, ReturnMessage msg) 
	{
		
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			
			DbcpPool.beginTran(cn);
			
			List list = null;

			//取得FORM中传入的查询参数
			Object[] fields2 = new Object[1];
			
			fields2[0] = theForm.getSeqID();
			
			JgShenqingServiceSqlMysql servSql = new JgShenqingServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			String sql = servSql.getbykeySql();			
					
			list = bSqlServ.dbRead(cn, sql, fields2, new DbJgShenqingService());
			
			if(list.size() ==0){
				common.setAppFailMsg(msg , " 取记录失败，请检查！");
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;				
			}
			
			sql = servSql.getDeleteSql();
			
			boolean  res = bSqlServ.dbWrite(cn, sql, fields2);
			if(!res){
				common.setAppFailMsg(msg , " 删除项目记录失败，请检查！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;
			}

			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);
			return;
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about info" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}

	/**
	 * 更新
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void update(JgShenqingForm theForm,ReturnMessage msg) 
	{
		
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			DbcpPool.beginTran(cn);

			
			//取得FORM中传入的查询参数
			Object[] fields = new Object[11];
			
			fields[0] = theForm.getJgType();
			fields[1] = theForm.getJgName();
			fields[2] = theForm.getVillageID();
			fields[3] = theForm.getContent();
			fields[4] = theForm.getUserID();
			fields[5] = theForm.getTel();
			fields[6] = theForm.getF_ID1();
			fields[7] = theForm.getF_ID2();
			fields[8] = theForm.getStatus();
			fields[9] = DatetimeUtil.getDatetime();
			fields[10] = theForm.getSeqID();
			
			JgShenqingServiceSqlMysql servSql = new JgShenqingServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			String sql = servSql.getUpdateSql();
						
			boolean  res = bSqlServ.dbWrite(cn, sql, fields);
			if(!res){
				common.setAppFailMsg(msg , " 更新失败！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return ;
			}
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);

		} catch(Exception ex) 
		{
			common.setAppFailMsg(msg , ex.getMessage());
			logger.error("System error , param get about info" + ex);
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}
	/**
	 * 更新状态
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void updsts(JgShenqingForm theForm,ReturnMessage msg) 
	{
		
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			DbcpPool.beginTran(cn);

			JgShenqingServiceSqlMysql servSql = new JgShenqingServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			//取得FORM中传入的查询参数
			Object[] fields1 = new Object[1];
			
			fields1[0] = theForm.getSeqID();

			String sql = servSql.getbykeySql();			
			
			List list = bSqlServ.dbRead(cn, sql, fields1, new DbJgShenqingService());
			if(list.size() ==0){
				common.setAppFailMsg(msg , " 取记录失败，请检查！");
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;				
			}
			BaseJgShenqing base = (BaseJgShenqing)list.get(0);
			
			//取得FORM中传入的查询参数
			Object[] fields = new Object[2];
			
			fields[0] = theForm.getStatus();
			fields[1] = theForm.getSeqID();
			
			sql = servSql.getUpdstsSql();
						
			boolean  res = bSqlServ.dbWrite(cn, sql, fields);
			if(!res){
				common.setAppFailMsg(msg , " 更新失败！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return ;
			}
			//审核通过，则建立对应的业委 物业
			if(theForm.getStatus().equals(GlbFinal.STS_NORMAL)){
				
				if(base.getJgType().equals(GlbFinal.JIGOU_YEWEI)){
					
					YeweiServiceImpl yeweiservice = new YeweiServiceImpl();
					YeweiForm yeweiForm = new YeweiForm();
					
					yeweiForm.setVillageID(base.getVillageID());
					yeweiForm.setTel(base.getTel());
					yeweiForm.setContactsID(base.getUserID());
					yeweiForm.setPepoleNum(1);
					
					yeweiservice.add(cn, yeweiForm, msg);
					if(msg.getErrCode() != 0){
						return;
					}
					
				}else if(base.getJgType().equals(GlbFinal.JIGOU_WUYE)){
					
					WuyeServiceImpl wuyeservice = new WuyeServiceImpl();
					WuyeForm wuyeForm = new WuyeForm();

					wuyeForm.setVillageID(base.getVillageID());
					wuyeForm.setTel(base.getTel());
					wuyeForm.setChairman(base.getUserID());
					wuyeForm.setPepoleNum(1);
					wuyeForm.setName(base.getJgName());
					wuyeForm.setInfo(base.getContent());
					
					wuyeservice.add(cn, wuyeForm, msg);
					if(msg.getErrCode() != 0){
						return;
					}
					
				}
				
			}
			
			
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);

		} catch(Exception ex) 
		{
			common.setAppFailMsg(msg , ex.getMessage());
			logger.error("System error , param get about info" + ex);
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}
}
